/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package entities;

import company.search.interfaces.SearchRequerimetsOR;
import java.io.Serializable;
import javax.persistence.Basic;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.Table;
import javax.validation.constraints.NotNull;
import javax.xml.bind.annotation.XmlRootElement;

/**
 *
 * @author damfercer
 */
@Entity
@Table(name = "search_relocating_info_requeriment_or_operator")
@XmlRootElement
@NamedQueries({
    @NamedQuery(name = "SearchRelocatingInfoRequerimentOrOperator.findAll", query = "SELECT s FROM SearchRelocatingInfoRequerimentOrOperator s"),
    @NamedQuery(name = "SearchRelocatingInfoRequerimentOrOperator.findByIdSearchRelocatingInfoRequerimentOrOperator", query = "SELECT s FROM SearchRelocatingInfoRequerimentOrOperator s WHERE s.idSearchRelocatingInfoRequerimentOrOperator = :idSearchRelocatingInfoRequerimentOrOperator")})
public class SearchRelocatingInfoRequerimentOrOperator implements Serializable, SearchRequerimetsOR {
    @Basic(optional = false)
    @NotNull
    @Column(name = "IMPORTANCE")
    private int importance;
    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Basic(optional = false)
    @Column(name = "ID_SEARCH_RELOCATING_INFO_REQUERIMENT_OR_OPERATOR")
    private Integer idSearchRelocatingInfoRequerimentOrOperator;
    @JoinColumn(name = "COUNTRY", referencedColumnName = "ID_COUNTRY")
    @ManyToOne(optional = false, fetch = FetchType.LAZY)
    private Country country;
    @JoinColumn(name = "SEARCH", referencedColumnName = "ID_SEARCH")
    @ManyToOne(optional = false, fetch = FetchType.LAZY)
    private Search search;
    @JoinColumn(name = "PROVINCE", referencedColumnName = "ID_PROVINCE")
    @ManyToOne(fetch = FetchType.LAZY)
    private Province province;

    public SearchRelocatingInfoRequerimentOrOperator() {
    }

    public SearchRelocatingInfoRequerimentOrOperator(Integer idSearchRelocatingInfoRequerimentOrOperator) {
        this.idSearchRelocatingInfoRequerimentOrOperator = idSearchRelocatingInfoRequerimentOrOperator;
    }

    public Integer getIdSearchRelocatingInfoRequerimentOrOperator() {
        return idSearchRelocatingInfoRequerimentOrOperator;
    }

    public void setIdSearchRelocatingInfoRequerimentOrOperator(Integer idSearchRelocatingInfoRequerimentOrOperator) {
        this.idSearchRelocatingInfoRequerimentOrOperator = idSearchRelocatingInfoRequerimentOrOperator;
    }

    public Country getCountry() {
        return country;
    }

    public void setCountry(Country country) {
        this.country = country;
    }

    public Search getSearch() {
        return search;
    }

    public void setSearch(Search search) {
        this.search = search;
    }

    public Province getProvince() {
        return province;
    }

    public void setProvince(Province province) {
        this.province = province;
    }

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (idSearchRelocatingInfoRequerimentOrOperator != null ? idSearchRelocatingInfoRequerimentOrOperator.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        boolean countryRes=false;
        boolean provinceRes=false;
        if (!(object instanceof SearchRelocatingInfoRequerimentOrOperator)) {
            return false;
        }
        SearchRelocatingInfoRequerimentOrOperator other = (SearchRelocatingInfoRequerimentOrOperator) object;
        
        //Sector test
        if(this.getCountry()==null && other.getCountry()==null){
            countryRes=true;
        }
        else if(this.getCountry()!=null && other.getCountry()!=null){
            countryRes=this.getCountry().equals(other.getCountry());
        }
        //MinimumEducationalLevel test
        if(this.getProvince()==null && other.getProvince()==null){
            provinceRes=true;
        }
        else if(this.getProvince()!=null && other.getProvince()!=null){
            provinceRes=this.getProvince().equals(other.getProvince());
        }
        
        Boolean res=countryRes && provinceRes;
        return res;
    }

    @Override
    public String toString() {
        return "entities.SearchRelocatingInfoRequerimentOrOperator[ idSearchRelocatingInfoRequerimentOrOperator=" + idSearchRelocatingInfoRequerimentOrOperator + " ]";
    }

    public int getImportance() {
        return importance;
    }

    public void setImportance(int importance) {
        this.importance = importance;
    }

    @Override
    public Integer getCandidateTotalPoints(Candidate c) {
        Integer finalPoints=0;
        Integer points;
        Integer countryPoints=20;
        Integer provincePoints=12;
        Integer requeriments=0;
        Integer candidateRequeriments;
        
        if(c.getCandidateRelocatingInfoList()!=null && !c.getCandidateRelocatingInfoList().isEmpty()){
            if(getCountry()!=null){
                requeriments++;
            }
            if(getProvince()!=null){
                requeriments++;
            }
            for(CandidateRelocatingInfo candidateRelocatingInfo:c.getCandidateRelocatingInfoList()){
                candidateRequeriments=0;
                points=0;
                if(getCountry()!=null){
                    if(candidateRelocatingInfo.getCountry()!=null && candidateRelocatingInfo.getCountry().equals(getCountry())){
                        points+=(countryPoints*importance);
                        candidateRequeriments++;
                    }
                }
                if(getProvince()!=null){
                    if(candidateRelocatingInfo.getProvince()!=null && candidateRelocatingInfo.getProvince().equals(getProvince())){
                        points+=(provincePoints*importance);
                        candidateRequeriments++;
                    }
                }
                if(requeriments<=candidateRequeriments){
                    points=points*2;
                }
                finalPoints+=points;
            }
        }
        return finalPoints;
    }
    
}
